package com.cfm880.market;

import android.Manifest;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;

import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.cfm880.market.adapter.AppRecyclerViewAdapter;
import com.cfm880.market.api.AppApi;
import com.cfm880.market.bean.App;
import com.orhanobut.logger.Logger;


import java.util.ArrayList;
import java.util.List;

import permissions.dispatcher.RuntimePermissions;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
    RecyclerView mRecyclerView;
    private static final int EXTERNAL_STORAGE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkAndRequestPermission();


        mRecyclerView = (RecyclerView) findViewById(R.id.app_recycview);
        final Configuration newConfig = getResources().getConfiguration();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://okok.qazpro.com:809/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        AppApi appService = retrofit.create(AppApi.class);
        appService.listApp("getAllApp")
                .enqueue(new Callback<List<App>>() {
            @Override
            public void onResponse(Call<List<App>> call, Response<List<App>> response) {
                List<App> apps = response.body();
                if (apps != null && apps.size() > 0 ) {
                    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
                        mRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
                    }else {
                        mRecyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,3,GridLayoutManager.VERTICAL, false));
                    }
                    mRecyclerView.setAdapter(new AppRecyclerViewAdapter(apps));

                } else {
                    Toast.makeText(getBaseContext(), "没有数据", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<List<App>> call, Throwable t) {
                Toast.makeText(getBaseContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
                Log.d("error", t.getMessage()+toString());
            }
        });

    }

    private void checkAndRequestPermission() {
        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
//            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
//                Logger.d("get Permission");
//            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE);
//            }
        }

        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
//            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)){
//                Logger.d("get Permission");
//            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},EXTERNAL_STORAGE);
//            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode){}
    }
}
